public class OutlookPasswordController {

    private final string PRODUCT_CONNECT_FOR_OUTLOOK = 'Salesforce Connect For Microsoft Outlook';
    private final string PRODUCT_SALESFORCE_FOR_OUTLOOK = 'Salesforce for Outlook and Blackberry';
    
    public OutlookPasswordController () {
    }
    
    private string password;
    public string GetPasswordField(){
        return null;
    }
    
    public void SetPasswordField(string pwd){
        password = pwd;
    }
    
    private string confirmPassword;
    public string GetConfirmPasswordField(){
        return null;
    }
    
    public void SetConfirmPasswordField(string pwd){
        confirmPassword= pwd;
    }

    public boolean isAllowedToResetPassword{
        get{
            return (
				UserProfileName.contains('National Sales') ||
				UserProfileName.contains('SFA OOR') ||
				UserProfileName.contains('IOG')
				);
        }
    }
    
    public string ProductName{
        get{
            if(UserProfileName.contains('SFA OOR'))
                ProductName = PRODUCT_SALESFORCE_FOR_OUTLOOK ;
            else
                ProductName = PRODUCT_CONNECT_FOR_OUTLOOK ;
                
            return ProductName;
        }
        set;
    }
    private string successMessage;
    public void SetSuccessMessage(string msg){
        successMessage= msg;
    }
    public string GetSuccessMessage(){
        return successMessage;
    }
    
    private string errorMessage;
    public void SetErrorMessage(string msg){
        errorMessage= msg;
    }
    public string GetErrorMessage(){
        return errorMessage;
    }
    
    public void Submit()
    {
        errorMessage = null;
        successMessage = null;
                    
        if(!password.equals(confirmPassword))
            errorMessage = 'Password confirmation string does not match the entered password.';
        else if(password.length() < 8){
            errorMessage = 'The password must be at least 8 characters long.';
        }
        else{
            if(!isAllowedToResetPassword){
                errorMessage = 'This feature is not avaialble for your profile. ';
                errorMessage += 'If you believe this feature should be available please contact a system ';
                errorMessage += 'administrator.';
            }
            else{
                try{
                System.setPassword(UserInfo.getUserId(), password);            
                }
                catch(Exception e){
                    errorMessage = e.getMessage();
                }
                if(errorMessage == null){
                    successMessage = GetProductSuccessMessage();
                }
            }
        }
    }

    private string GetProductSuccessMessage(){
        string message;
        
        if(PRODUCT_CONNECT_FOR_OUTLOOK == ProductName){
            message= 'Your Salesforce Connect for Microsoft Outlook password has been set. ';
            message+= 'Check your e-mail, you should receive a new security token from salesforce.com. ';
            message+= 'Once you receive your new security token, open Outlook and go to the Salesforce.com options. ';
            message+= 'In Outlook verify that the username field is set to ' + UserInfo.getUsername();
            message+= ' and update the Salesforce password field to be your new password suffixed with the security token. ';
            message+= 'For example, if your password is "mypassword" ';
            message+= 'and your security token is "XXXXXXXXXX" ';
            message+= 'then you must enter "mypasswordXXXXXXXXXX" in the password field.';
        }      
        else if(PRODUCT_SALESFORCE_FOR_OUTLOOK == ProductName){
            if(isProduction){
            message= 'Your Salesforce for Outlook and Blackberry password has been set.  Now you can configure these applications.<BR>';
            message+= '<BR>';
            message+= 'Salesforce for Outlook:<BR>';
            message+= '1)   Right-click the Salesforce for Outlook icon in the system tray and click Settings<BR>';
            message+= '2)   Enter the following information and click Login<BR>';
            message+= 'Connect to:  https://www.salesforce.com<BR>';
            message+= 'Username: ' + UserInfo.getUsername() + '<BR>';
            message+= 'Password:  The same password you just entered<BR>';
            message+= '<BR>';
            message+= '3)   Click Next then Close to finish setup.<BR>';
            message+= '<BR>';
            message+= 'Blackberry mobile:<BR>';
            message+= '1)   Click the Salesforce Mobile icon, which resides on the home page of your device or in the Applications folder<BR>';
            message+= '2)   Read the license agreement and click I Accept<BR>';
            message+= '3)   If you previously used your blackberry on the TEST server, see <A href="https://na1.salesforce.com/_ui/training/help/pub/UserEdSolution?id=50130000000MBZx&ps=1&orgId=00D000000000062">here</A> to point your blackberry to production (this may not apply to you)<BR>';
            message+= '4)   Enter your username and password:<BR>';
            message+= 'Username: ' + UserInfo.getUsername() + '<BR>';
            message+= 'Password:  The same password you just entered<BR>';
            message+= '5)   Click Activate to verify your Salesforce.com account and activate the mobile application on the device<BR>';
            message+= 'After your username and password are confirmed by Salesforce.com, the data downloads<BR>';
            }
            else{
            message= 'Your Salesforce for Outlook and Blackberry password has been set.  Now you can configure these applications.<BR>';
            message+= '<BR>';
            message+= 'Salesforce for Outlook:<BR>';
            message+= '1)   Right-click the Salesforce for Outlook icon in the system tray and click Settings<BR>';
            message+= '2)   Enter the following information and click Login<BR>';
            message+= 'Connect to:  https://test.salesforce.com<BR>';
            message+= 'Username: ' + UserInfo.getUsername() + '<BR>';
            message+= 'Password:  The same password you just entered<BR>';
            message+= '<BR>';
            message+= '3)   Click Next then Close to finish setup.<BR>';
            message+= '<BR>';
            message+= 'Blackberry mobile:<BR>';
            message+= '1)   Click the Salesforce Mobile icon, which resides on the home page of your device or in the Applications folder<BR>';
            message+= '2)   Read the license agreement and click I Accept<BR>';
            message+= '3)   Click the "Menu" button<BR>';
            message+= '4)   Select "System Info"<BR>';
            message+= '5)   Select on "Support Command"<BR>';
            message+= '6)   Type "LZSAND" in the command window and send it<BR>';
            message+= '7)   Enter your username and password:<BR>';
            message+= 'Username: ' + UserInfo.getUsername() + '<BR>';
            message+= 'Password:  The same password you just entered<BR>';
            message+= '8)   Click Activate to verify your Salesforce.com account and activate the mobile application on the device<BR>';
            message+= 'After your username and password are confirmed by Salesforce.com, the data downloads<BR>';
            }
        }    
        return message;
    }    
    private booLean isProduction{
        get{
            ID OrgId = [Select Id from Organization limit 1].Id;
            return (orgid == '00D300000000ITKEA2') ;
        }
    }
    private string UserProfileName{
        get{
            if(UserProfileName == null){
                string profileId = UserInfo.getProfileId(); 
                Profile p = [Select name from profile where id = :profileId ];
                UserProfileName = p.Name;
            }
                return UserProfileName;
        }
        set;
    }
}